package org.flashbrighton.as3bots.logic
{
	import org.flashbrighton.as3bots.collision.CollidableType;
	import org.flashbrighton.as3bots.events.BotEvent;

	public class SebsBotLogic implements IBotLogic
	{
		
		private var rotateCounter : int;
		
		private var reverseCounter : int;  
		
		private var _rotatorPercent : Number ; 
		private var _enginePercent : Number ; 
		
		private var damageCounter : Array; 
		
		private var rotationAdjustment : Number; 
		
		public function SebsBotLogic()
		{
			
			rotateCounter = 0; 
			reverseCounter = 0; 
			_enginePercent = 1; 
			rotationAdjustment = 1; 
			damageCounter = new Array(); 
			turnAround(Math.random()*10); 
			
		}

		public function run():void
		{
			if(rotateCounter>0)
			{
				rotateCounter--; 
				if(rotateCounter==0) 
				{
					stopRotation();
					_enginePercent=1; 
				}
			}
			
			if(reverseCounter>0)
			{
				reverseCounter--; 
				if(reverseCounter ==1)
				{
					_rotatorPercent = rotationAdjustment; 
					
				} 
				else if(reverseCounter ==0) 
				{
					stopRotation(); 
					_enginePercent = 1; 
				
				}
				
			}
			
		}
		public function stopRotation() : void
		{
			_rotatorPercent = (Math.random()*0.6) - 0.3; 
			
		
		}
		
		public function notify(event:BotEvent):void
		{
			if(event.targetType==CollidableType.WALL)
			{
				if(reverseCounter>0) 
				{
					reverseCounter = 0; 
					_enginePercent = 0 ; 
					turnAround(4); 	
				}
				else if(rotateCounter>0)
				{
					_rotatorPercent = 0; 
					_enginePercent = 1; 
					rotateCounter = 0; 
					
					
				}
				else
				{
					turnAround(); 
				}
			}
			if(event.targetType== CollidableType.BOT)
			{
				
				reverse(); 
				if(damageCounter[damageCounter.length-1]>event.points)
				{
					rotationAdjustment = -rotationAdjustment; 
				}
			
				damageCounter.push(event.points); 
				
			}
		}
		
		
		public function turnAround(rotatecounter:int = 7) : void
		{
			
			var variation:Number = (Math.random()*4)-2; 
			rotateCounter +=variation; 
			
			rotateCounter = rotatecounter; 
			_rotatorPercent = 1; 
			_enginePercent = 0; 
			
			
		}
		
		public function reverse() : void
		{
			if(reverseCounter==0)
			{
				reverseCounter = 2 + Math.random()*3; 
				_enginePercent = -1; 
			} 
			else
			{
				reverseCounter = 0; 
				_enginePercent = 1; 
				
				
			}
		}
		
		public function get enginePercent():Number
		{
			return _enginePercent;
		}
		
		public function get rotatorPercent():Number
		{
			return _rotatorPercent;
		}
		
		
	}
}